package data.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import connexionBd.ConnexionBd;
import data.bean.Equipement;

public class EquipementDAO {
	
	private final static Connection connexion = ConnexionBd.getConnection();

	public static Equipement select(int idEquipement) {
		Equipement equipement = null;
		try {
			Statement statement = connexion.createStatement();
			String sql = "SELECT * FROM energie.equipement WHERE id_equipement = " + idEquipement;
			ResultSet result = statement.executeQuery(sql);
			if (result.first()) {
				equipement = new Equipement(idEquipement,
						result.getString("libelle_equipement"),
						result.getString("infos_equipement"));
			}
			result.close();
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return equipement;
	}

	public static boolean insert(Equipement equipement) {
		boolean result = false;
		try {
			Statement statement = connexion.createStatement();
			String sql = "INSERT INTO energie.equipement "
					+ "(id_equipement, libelle_equipement, infos_equipement) "
					+ "VALUES (" + equipement.getId() + ", "
							+ "'" + equipement.getLibelle() + "', "
							+ "'" + equipement.getInfos() + "')";
			int statut = statement.executeUpdate(sql);
			result = (statut == 1);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String getInsertRequete(Equipement equipement) {
			String sql = "INSERT INTO energie.equipement "
					+ "(id_equipement, libelle_equipement, infos_equipement) "
					+ "VALUES (" + equipement.getId() + ", "
							+ "'" + equipement.getLibelle() + "', "
							+ "'" + equipement.getInfos() + "')";
		return sql;
	}

	public static boolean delete(int idEquipement) {
		boolean result = false;
		try {
			Statement statement = connexion.createStatement();
			String sql = "DELETE FROM energie.equipement WHERE id_equipement = " + idEquipement;
			int statut = statement.executeUpdate(sql);
			result = (statut == 1);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
